package com.itheima;

/**
 * @auther Macrydy
 * @date 2020/7/7 14:25
 */
public class Ticket implements Runnable {

    private Object obj = new Object();
    private int ticket = 100;

    @Override
    public void run() {

        while (true) {
            synchronized (obj) {
                if (ticket>0){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    ticket--;
                    System.out.println(Thread.currentThread().getName() + "正在卖票，还剩余" + ticket + "张");
                }else {
                    return;
                }

            }
        }

    }
}
